@model WorkflowTypeIndexViewModel
@using OrchardCore.Workflows.ViewModels;

@{
    int startIndex = (Model.Pager.Page - 1) * (Model.Pager.PageSize) + 1;
    int endIndex = startIndex + Model.WorkflowTypes.Count - 1;
}
<script asp-name="bootstrap-select" depends-on="admin" at="Foot"></script>

<zone Name="Title"><h1>@RenderTitleSegments(T["Manage Workflows"])</h1></zone>

<form asp-action="Index" id="workflows-types-form" method="post" class="no-multisubmit">
    <input type="submit" name="submit.Filter" id="submitFilter" class="visually-hidden" />
    <input type="submit" name="submit.BulkAction" class="visually-hidden" />
    <input asp-for="Options.BulkAction" type="hidden" />

    <div class="card mb-3 position-sticky action-bar">
        <div class="card-body bg-primary p-3">
            <div class="mb-0 has-search">
                <div class="input-group input-group-sm w-sm-50 w-25 d-inline-flex has-search">
                    <i class="fa fa-search form-control-feedback" aria-hidden="true"></i>
                    <input asp-for="Options.Search" class="form-control" placeholder="@T["Search"]" type="search" autofocus />
                </div>
                <div class="btn-group float-end">
                    <a asp-action="EditProperties" asp-route-returnUrl="@FullRequestPath" class="btn btn-sm btn-secondary float-end create" role="button">@T["Create Workflow"]</a>
                </div>
            </div>
        </div>
    </div>
    <ul class="list-group with-checkbox">
        @if (Model.WorkflowTypes.Any())
        {
            <li class="list-group-item bg-primary">
                <div class="row">
                    <div class="col mb-1 text-nowrap">
                        <div class="form-check mt-2 me-n2">
                            <input type="checkbox" class="form-check-input" id="select-all">
                            <label class="form-check-label" for="select-all" id="select-all-label" title="@T["Select All"]"></label>
                            <label id="items" for="select-all">@T.Plural(Model.WorkflowTypes.Count, "1 item", "{0} items")<span class="text-muted" title="@T["Items {0} to {1}", startIndex, endIndex]">@T.Plural((int)Model.Pager.TotalItemCount, " / {0} item in total", " / {0} items in total")</span></label>
                            <label id="selected-items" class="text-muted" for="select-all"></label>
                        </div>
                    </div>
                    <div class="col-2 mb-1" style="display:none" id="actions">
                        <div class="dropdown float-end mt-1">
                            <button class="btn btn-sm btn-light dropdown-toggle" type="button" id="bulk-action-menu-button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                @T["Actions"]
                            </button>
                            <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="bulk-action-menu-button">
                                @foreach (var item in Model.Options.WorkflowTypesBulkAction)
                                {
                                    <li><a class="dropdown-item" href="javascript:void(0)" data-action="@item.Value" data-title="@T["Bulk Action"]" data-message="@T["Are you sure you want to {0} these items?", @item.Text.ToLower()]">@item.Text</a></li>
                                }

                                <li><a class="dropdown-item" href="javascript:void(0)" data-action="export" data-title="@T["Bulk Action"]">Export</a> </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </li>
            @for (var i = 0; i < Model.WorkflowTypes.Count; i++)
            {
                var entry = Model.WorkflowTypes[i];
                <li class="list-group-item">
                    <div class="properties">
                        <div class="float-end">
                            <a asp-action="Edit" asp-route-id="@entry.WorkflowType.Id" asp-route-returnUrl="@FullRequestPath" class="btn btn-primary btn-sm">@T["Edit"]</a>
                            <a asp-action="EditProperties" asp-route-id="@entry.WorkflowType.Id" asp-route-returnUrl="@FullRequestPath" class="btn btn-success btn-sm">@T["Properties"]</a>
                            <a asp-action="Delete" asp-route-id="@entry.WorkflowType.Id" class="btn btn-danger btn-sm" data-url-af="RemoveUrl UnsafeUrl">@T["Delete"]</a>
                            <a asp-action="Duplicate" asp-route-id="@entry.WorkflowType.Id" asp-route-returnUrl="@FullRequestPath" class="btn btn-warning btn-sm">@T["Duplicate"]</a>
                        </div>

                        <div class="form-check float-start">
                            <input type="checkbox" class="form-check-input" value="@entry.Id" name="itemIds" id="itemIds-@entry.Id">
                            <label class="form-check-label" for="itemIds-@entry.Id"></label>
                        </div>
                        <a asp-action="Edit" asp-route-id="@entry.WorkflowType.Id" asp-route-returnUrl="@FullRequestPath">@entry.Name</a>
                        @if (!entry.WorkflowType.IsEnabled)
                        {
                            <span class="badge text-danger"><i class="fas fa-power-off" aria-hidden="true"></i> @T["Disabled"]</span>
                        }
                        @if (entry.WorkflowCount > 0)
                        {
                            <a asp-action="Index" asp-controller="Workflow" asp-route-workflowtypeid="@entry.WorkflowType.Id" class="badge ta-badge"><i class="fa fa-list-ol" aria-hidden="true"></i> @T.Plural(entry.WorkflowCount, "1 instance", "{0} instances")</a>
                        }

                        <div class="metadata">
                        </div>
                    </div>
                </li>
            }
        }
        else
        {
            <li class="list-group-item">
                <div class="alert alert-info mb-0" role="alert">
                    @T["<strong>Nothing here!</strong> There are no workflows for the moment."]
                </div>
            </li>
        }
    </ul>
</form>
@await DisplayAsync(Model.Pager)

<script at="Foot">
    $(function () {
        $('select').selectpicker();

        var actions = $("#actions");
        var items = $("#items");
        var filters = $(".filter");
        var selectAllCtrl = $("#select-all");
        var selectedItems = $("#selected-items");
        var itemsCheckboxes = $(":checkbox[name='itemIds']");

        $(".filter-options input").on("change", function () {
            $("[name='submit.Filter']").click();
        });

        $(".dropdown-menu .dropdown-item").filter(function() {
            return $(this).data("action");
        }).on("click", function () {
            if ($(":checkbox[name='itemIds']:checked").length >= 1) {
                var $this = $(this);
                 if ($this.data("action")==="export"){
                   var ids=[]
                   $(":checkbox[name='itemIds']:checked").each(function (){
                       ids.push(this.value)
                   });
                   window.open('@Href("~/EasyOC.Workflows/Workflow/BulkExport?ids=")'+ids)
                 }else{
                   confirmDialog({title: $this.data('title'), message: $this.data('message'), callback: function(r) {
                         if (r) {
                             $("[name='Options.BulkAction']").val($this.data("action"));
                             $("[name='submit.BulkAction']").click();
                         }
                     }});
                 }
                }
        });

        function displayActionsOrFilters() {
            if ($(":checkbox[name='itemIds']:checked").length >= 1) {
                actions.show();
                filters.hide();
                selectedItems.show();
                items.hide();
            }
            else {
                actions.hide();
                filters.show();
                selectedItems.hide();
                items.show();
            }
        }

        selectAllCtrl.click(function(){
            itemsCheckboxes.not(this).prop("checked", this.checked);
            selectedItems.text($(":checkbox[name='itemIds']:checked").length + ' @T["selected"]');
            displayActionsOrFilters();
        });

        itemsCheckboxes.on("click", function () {
            var itemsCount = $(":checkbox[name='itemIds']").length;
            var selectedItemsCount = $(":checkbox[name='itemIds']:checked").length;

            selectAllCtrl.prop("checked", selectedItemsCount == itemsCount);
            selectAllCtrl.prop("indeterminate", selectedItemsCount > 0 && selectedItemsCount < itemsCount);

            selectedItems.text(selectedItemsCount + ' @T["selected"]');
            displayActionsOrFilters();
        });
    });
</script>
